package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.api.common.utils.StringUtil;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.constant.LBConstant;
import com.dwxt.cdms.dao.*;
import com.dwxt.cdms.entity.LbWatchData;
import com.dwxt.cdms.entity.vo.LbWatchWarnVO;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LbWatchDataService {


    private final  LbWatchDataMapper lbWatchDataMapper;
    private final StationService stationService;
    private final UserServiceAdmin userServiceAdmin;
    private final LbWatchSleepMapper sleepMapper;
    private final LbWatchStepMapper stepMapper;
    private final LbWatchLocMapper locMapper;
    private final LbWatchWarnMapper warnMapper;

    public BaseResult page(Page page, Integer customerId, String type) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        //不带类型不返回数据
        if (StringUtil.isBlank(type)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        List list = new ArrayList();
        //判断类型
        if (type.equals(LBConstant.location)) {
            //定位
            list = locMapper.selectPageByCustomerId(customerId);
        } else if (type.equals(LBConstant.sleep)) {
            //睡眠
            list = sleepMapper.selectPageByCustomerId(customerId);
        } else if (type.equals(LBConstant.step)) {
            //计步
            list = stepMapper.selectPageByCustomerId(customerId);;
        } else {
            //血压、血氧、心率
            list = lbWatchDataMapper.selectPageByCustomerId(customerId);;
        }
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult add(LbWatchData lbWatchData) {
        lbWatchDataMapper.insertSelective(lbWatchData);
        return BaseResult.ok();
    }

    public BaseResult delete(Integer id) {
        lbWatchDataMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public BaseResult update(LbWatchData id) {
        lbWatchDataMapper.updateByPrimaryKeySelective(id);
        return BaseResult.ok();
    }

    public LbWatchData getLbWatchDataById(Integer id) {
        return lbWatchDataMapper.selectByPrimaryKey(id);
    }

    public BaseResult pageGroup(Page page, String name,String type) {
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        //不带类型不返回数据
        if (StringUtil.isBlank(type)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        List list = new ArrayList();
        //判断类型
        if (type.equals(LBConstant.location)) {
            //定位
            list = locMapper.selectGroup(name,stationId);
        } else if (type.equals(LBConstant.sleep)) {
            //睡眠
            list = sleepMapper.selectGroup(name,stationId);
        } else if (type.equals(LBConstant.step)) {
            //计步
            list = stepMapper.selectGroup(name,stationId);
        } else {
            //血压、血氧、心率
            list = lbWatchDataMapper.selectGroup(name,stationId);
        }
        return BaseResult.ok(new PageInfo(list));
    }


    public BaseResult selectLbWarnPage(Page page,LbWatchWarnVO lbWatchWarnVO) {
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<LbWatchWarnVO> list = warnMapper.selectLbWarnPage(lbWatchWarnVO);
        return BaseResult.ok(new PageInfo(list));
    }
}
